package no.ntnu.fp.watch.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import no.ntnu.fp.model.Database;
import no.ntnu.fp.model.Platform;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RealTimeCommentDialog extends JDialog {
	
	private JTextArea commentField;
	private JButton saveButton;
	private JButton cancelButton;
	private JLabel commentTextLabel;
	
	private Platform platform;

	public RealTimeCommentDialog(Platform p){
		
		this.platform = p;
		
		initGUI();
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setAlwaysOnTop(true);
		this.setTitle("Add a real time comment");
		this.setVisible(true);

	}
	
	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			{
				cancelButton = new JButton();
				getContentPane().add(cancelButton, new AnchorConstraint(841, 619, 943, 491, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				cancelButton.setText("Cancel");
				cancelButton.setPreferredSize(new java.awt.Dimension(68, 28));
				cancelButton.addActionListener(new cancelButtonListener(this));
			}
			{
				saveButton = new JButton();
				getContentPane().add(saveButton, new AnchorConstraint(841, 485, 943, 358, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				saveButton.setText("Save");
				saveButton.setPreferredSize(new java.awt.Dimension(68, 28));
				saveButton.addActionListener(new saveButtonListener(this));
			}
			{
				commentTextLabel = new JLabel();
				getContentPane().add(commentTextLabel, new AnchorConstraint(60, 534, 118, 188, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				commentTextLabel.setText("Enter a comment:");
				commentTextLabel.setPreferredSize(new java.awt.Dimension(184, 16));
			}
			{
				commentField = new JTextArea();
				getContentPane().add(commentField, new AnchorConstraint(140, 861, 827, 143, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				commentField.setPreferredSize(new java.awt.Dimension(382, 189));
				commentField.setBorder(new EtchedBorder());
			}
			pack();
			this.setSize(609, 408);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	
	class saveButtonListener implements ActionListener{
		
		private RealTimeCommentDialog dialog;
		
		public saveButtonListener(RealTimeCommentDialog d){
			this.dialog = d;
 		}
		
		
		public void actionPerformed(ActionEvent e){
			Database db = new Database();
			if (!commentField.getText().isEmpty()) {
				this.dialog.dispose();
				db.insertRealTimeComment(commentField.getText(), platform.getCurrentWell().getId());
				JOptionPane.showMessageDialog(dialog, "Comment was successfully saved");
				
			}
			else{
				JOptionPane.showMessageDialog(dialog, "You must write a comment before saving");
			}
			
			
		}
	}
	
	class cancelButtonListener implements ActionListener{
		private RealTimeCommentDialog dialog;
		
		public cancelButtonListener(RealTimeCommentDialog d){
			this.dialog = d;
		}
		
		public void actionPerformed(ActionEvent e){
			this.dialog.dispose();
		}
	}
	
	

}
